﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Common.statics;
using System.Web.UI.WebControls;
using System.Web;
using Enter.Common.internals;

namespace Enter.Common.Upload
{
	public class UploadUnit
	{
		private UploadUnitType _uploadtype = UploadUnitType.成功;

		public UploadUnitType Uploadtype
		{
			get { return _uploadtype; }
			set { _uploadtype = value; }
		}

		private bool _IsSuccess = false;

		/// <summary>
		/// 是否有图片生成
		/// </summary>
		public bool IsSuccess
		{
			get { return _IsSuccess; }
			set { _IsSuccess = value; }
		}

		private string _uploadFileExt = string.Empty;
		private string _uploadPath = string.Empty;

		private string _uploadFormat = "yyyyMM";
		private bool _IsThumbnail = false;

		private bool _IsSwf = false;

		private bool _IsOther = false;

		private string _OtherExt = "";

		/// <summary>
		/// 其他文件扩展名 .swf|.fla 
		/// </summary>
		public string OtherExt
		{
			get { return _OtherExt; }
			set { _OtherExt = value; }
		}

		/// <summary>
		/// 是否可以上传其他文件
		/// </summary>
		public bool IsOther
		{
			get { return _IsOther; }
			set { _IsOther = value; }
		}

		/// <summary>
		/// 是否可以上传Falsh
		/// </summary>
		public bool IsSwf
		{
			get { return _IsSwf; }
			set { _IsSwf = value; }
		}



		/// <summary>
		/// 是否生成缩微图
		/// </summary>
		public bool IsThumbnail
		{
			get { return _IsThumbnail; }
			set { _IsThumbnail = value; }
		}

		public string UploadFormat
		{
			get { return _uploadFormat; }
			set { _uploadFormat = value; }
		}

		private int _uploadLength = 1024000;

		/// <summary>
		/// 最大图像大小
		/// </summary>
		public int UploadLength
		{
			get { return _uploadLength; }
			set { _uploadLength = value; }
		}

		private int[] _uploadWith = null;
		/// <summary>
		/// 缩微图宽
		/// </summary>
		public int[] UploadWith
		{
			get { return _uploadWith; }
			set { _uploadWith = value; }
		}
		private int[] _uploadHigth = null;

		/// <summary>
		/// 缩微图高
		/// </summary>
		public int[] UploadHigth
		{
			get { return _uploadHigth; }
			set { _uploadHigth = value; }
		}

		/// <summary>
		/// 图像存储路径
		/// </summary>
		public string UploadPath
		{
			get { return _uploadPath; }
			set { _uploadPath = value; }
		}


		public UploadUnit()
		{

		}

		public UploadUnit(string uploadPath)
		{
			this._uploadPath = uploadPath;
		}


		#region FileUpload
		public string UpLoadImg(FileUpload fileUpload)
		{
			return UpLoadImg(fileUpload.PostedFile);
		}

		#endregion


		#region HttpPostedFile

		public string UpLoadImg(HttpPostedFile fileUpload)
		{
			string newfile = string.Empty;

			if ((CheckFile()) && (CheckImg(fileUpload)))
			{
				newfile = SaveImg(fileUpload);
			}
			// Uploadtype = _uploadtype;
			return newfile;
		}

		#endregion




		#region private
		private bool CheckFile()
		{
			if (!SafeCheck.IsMap(_uploadPath))
			{
				_uploadPath = FilesStatic.GetFolderMapPath(_uploadPath);
			}

			FilesStatic.CreatedFolder(_uploadPath);

			if (FilesStatic.IsFolder(_uploadPath))
			{
				return true;
			}
			else
			{
				_uploadtype = UploadUnitType.存储文件夹错误;
				return false;

			}
		}

		private bool CheckImg(HttpPostedFile fileUpload)
		{

			if (fileUpload.ContentLength == 0)
			{
				_uploadtype = UploadUnitType.无文件上传;
				return false;
			}

			if (fileUpload.ContentLength > _uploadLength)
			{
				_uploadtype = UploadUnitType.文件过大;
				return false;
			}


			string Files = fileUpload.FileName;

			if ((_uploadWith != null) && (_uploadHigth != null))
			{
				if ((_uploadWith.Length > 0) && ((_uploadHigth.Length > 0)) && (_uploadWith.Length == _uploadHigth.Length))
				{
					_IsThumbnail = true;
				}
			}

			if (!CheckExt(Files))
			{
				_uploadtype = UploadUnitType.文件格式错误;
				return false;
			}
			this._uploadFileExt = (FilesStatic.GetExtension(Files)).ToLower();
			return true;
		}

		/// <summary>
		/// 检查文件是否可上传文件
		/// </summary>
		/// <param name="Files"></param>
		/// <returns></returns>
		private bool CheckExt(string Files)
		{
			bool res = false;
			if (SafeCheck.IsJPGstr(Files))
			{
				res = true;
			}
			else
			{
				_IsThumbnail = false;
				if (_IsSwf)
				{
					if (SafeCheck.IsSwfstr(Files))
					{
						res = true;
					}
				}
				if (_IsOther)
				{
					if (SafeCheck.IsOther(Files, OtherExt))
					{
						res = true;
					}
				}
			}

			return res;
		}

		/// <summary>
		/// 保存图片 
		/// </summary>
		/// <param name="fileUpload"></param>
		/// <returns></returns>
		private string SaveImg(HttpPostedFile fileUpload)
		{
			string NewFile = string.Empty;

			try
			{
				string result = string.Empty;

				string date = DateTime.Now.Ticks.ToString();

				string filedate = string.Empty;
				string dateFormat = DateTime.Now.ToString(_uploadFormat);

				string datapathyuan = FilesStatic.CreatedFolderData(_uploadPath, dateFormat, out filedate);

				string fileName = filedate + date + _uploadFileExt;

				///生成缩微图
				fileUpload.SaveAs(_uploadPath + fileName);
				result = fileName;
				_IsSuccess = true;
				if (IsThumbnail)
				{
					result = filedate + date + _uploadFileExt;
					Thumbnail thumbnail = null;
					for (int i = 0, count = _uploadWith.Length; i < count; i++)
					{
						thumbnail = new Thumbnail(_uploadPath + fileName);
						NewFile = filedate + date + "_" + i + _uploadFileExt;
						thumbnail.thumbnailFile(_uploadPath + NewFile, _uploadWith[i], _uploadHigth[i]);
					}

				}

				return result;
			}
			catch
			{

				_uploadtype = UploadUnitType.保存文件出错;
				return "";
			}
		}


		#endregion

	}


	public enum UploadUnitType
	{
		存储文件夹错误 = 1,
		文件过大 = 2,
		文件格式错误 = 3,
		保存文件出错 = 4,
		成功 = 5,
		无文件上传 = 6,
	}
}
